#include "GameView.h"
#include "Game.h"
#include "Race.h"
#include "TrackActor.h"
#include "Actor.h"
#include "JetActor.h"
#include "Jet.h"

extern DebugTracer _DEBUGTRACER;
GameView::GameView(Ogre::RenderWindow *window,Ogre::SceneManager* scene,Game *game)
{
    m_game = NULL;
    m_mainActor = NULL;
    m_window = NULL;
    m_currentCamera = NULL;
    m_viewport = NULL;
    m_debugContainer = NULL;
    m_debugOverlayPos = NULL;
    m_debugOverlayVel = NULL;
    m_debugOverlay = NULL;

    _DEBUGTRACER.Print("initialising overlay elements");
    m_debugOverlay = Ogre::OverlayManager::getSingleton().create("debug_overlay");
    m_debugContainer = (Ogre::OverlayContainer*)Ogre::OverlayManager::OverlayManager::getSingleton().createOverlayElement("Panel","debug_container");
    m_debugOverlayPos = (Ogre::TextAreaOverlayElement*)Ogre::OverlayManager::getSingleton().createOverlayElement("TextArea","debug_position");
    m_debugOverlayVel = (Ogre::TextAreaOverlayElement*)Ogre::OverlayManager::getSingleton().createOverlayElement("TextArea","debug_velocity");

    m_debugContainer->initialise();
    m_debugOverlayPos->initialise();
    m_debugOverlayVel->initialise();
    _DEBUGTRACER.Print("ovelay elements created");
    m_debugContainer->setPosition(20,20);
    m_debugOverlay->add2D(m_debugContainer);

    m_debugContainer->addChild(m_debugOverlayPos);
    m_debugOverlayPos->setAlignment(m_debugOverlayPos->Left);
    m_debugOverlayPos->setFontName("debug_font");
    m_debugOverlayPos->setCharHeight(12);
    m_debugOverlayPos->setColour(Ogre::ColourValue(1.0,1.0,1.0));
    m_debugOverlayPos->setPosition(0,0);

    m_debugContainer->addChild(m_debugOverlayVel);
    m_debugOverlayVel->setAlignment(m_debugOverlayVel->Left);
    m_debugOverlayVel->setFontName("debug_font");
    m_debugOverlayVel->setCharHeight(12);
    m_debugOverlayVel->setColour(Ogre::ColourValue(1.0,1.0,1.0));
    m_debugOverlayVel->setPosition(0,15);

    _DEBUGTRACER.Print("overlays done");

    m_game = game;
    m_window = window;


    //initializing the key binding
    m_keyBinding[K_ACCELERATE] = sf::Keyboard::Up;
    m_keyBinding[K_BRAKE] = sf::Keyboard::Down;
    m_keyBinding[K_TURNLEFT] = sf::Keyboard::Left;
    m_keyBinding[K_TURNRIGHT] = sf::Keyboard::Right;


    //initalizing the actors
    std::vector<Player*>::iterator it;
    Actor* actor = NULL;
    for(it = game->GetRace()->GetPlayerList().begin(); it < game->GetRace()->GetPlayerList().end(); it++)
    {
        if((*it)->GetType() == COMPUTER)
            actor = new ComputerPlayerActor(scene,(*it));
        else
        {
            actor = new HumanPlayerActor(scene,(*it));
            std::string temp = "player";
            std::string name = (*it)->GetName();

            temp += name;
        }
        m_actorList.push_back(actor);
    }
    actor = new TrackActor(scene,game->GetRace()->GetTrack());
    m_actorList.push_back(actor);

    SetActor(m_actorList[0]);
}
void GameView::Update(float timedelta)
{
    if(sf::Keyboard::IsKeyPressed(m_keyBinding[K_ACCELERATE]))
        m_game->OnAccelerateKey(timedelta);

    if(sf::Keyboard::IsKeyPressed(m_keyBinding[K_BRAKE]))
        m_game->OnBrakeKey(timedelta);

    if(sf::Keyboard::IsKeyPressed(m_keyBinding[K_TURNLEFT]))
        m_game->OnTurnLeftKey(timedelta);

    if(sf::Keyboard::IsKeyPressed(m_keyBinding[K_TURNRIGHT]))
        m_game->OnTurnRightKey(timedelta);



    for(unsigned int i = 0; i < m_actorList.size(); i++)
    {
        m_actorList[i]->Update(timedelta);
    }
        _DEBUGTRACER.Print("actor is a player");
        Ogre::String text = "x: ";
        HumanPlayerActor* actor = (HumanPlayerActor*)(m_mainActor);
        _DEBUGTRACER.Print("actor conversion done");
        Jet* jet = actor->GetJetActor()->GetJet();
        _DEBUGTRACER.Print("jet is correctly set");

        char buffer[10];
        sprintf(buffer,"%f",jet->GetPosition().x);

        text += buffer;
        text += "    y: ";

        sprintf(buffer,"%f",jet->GetPosition().y);

        text += buffer;
        text += "   z: ";

        sprintf(buffer,"%f",jet->GetPosition().z);

        text += buffer;
        m_debugOverlayPos->setCaption(text);

        text.clear();
        text += "vx: ";

        sprintf(buffer,"%f",jet->GetSpeed().x);

        text += buffer;
        text += "    vy: ";

        sprintf(buffer,"%f",jet->GetSpeed().y);

        text += buffer;
        text += "    vz: ";

        sprintf(buffer,"%f",jet->GetSpeed().z);
        text += buffer;

        m_debugOverlayVel->setCaption(text);
        _DEBUGTRACER.Print("captions set");
}
GameView::~GameView()
{
    //dtor
}
void GameView::SetActor(Actor *actor)
{
    Ogre::Camera *camera = actor->GetActiveCamera();

    if(actor != NULL && actor->GetActiveCamera() != NULL)
    {
        m_window->removeAllViewports();
        m_viewport = m_window->addViewport(actor->GetActiveCamera());
        m_viewport->setBackgroundColour(Ogre::ColourValue(0,0,0));
        camera->setAspectRatio(Ogre::Real(m_viewport->getActualWidth())/Ogre::Real(m_viewport->getActualHeight()));
        m_mainActor = actor;
    }

}
